﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.UI.WebControls;
using ERMSys.Framework.Utilities;
using Se7en.Admin.Business;
using Se7en.Admin.Common;
using dal = Se7en.Admin.Model;

namespace Se7en.Admin.Web.Pages
{
    public partial class ManageMenu : AdminPageBase
    {
        #region Properties

        private List<dal.Menu> _DataSource;
        private List<dal.Menu> DataSource
        {
            get
            {
                if (_DataSource != null)
                    return _DataSource;
                else
                    _DataSource = ViewState["DataSource"] as List<dal.Menu>;

                if (_DataSource == null)
                    this.DataSource = new List<dal.Menu>();

                return _DataSource;
            }
            set
            {
                ViewState["DataSource"] = _DataSource = value;
            }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            lblError.Text = "";
            if (!IsPostBack)
            {
                BindMenus();
            }
        }

        protected void grdMenu_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                ImageButton btnImgUp = (ImageButton)e.Row.FindControl("btnImgUp");
                ImageButton btnImgDown = (ImageButton)e.Row.FindControl("btnImgDown");

                if (e.Row.RowIndex == 0)
                    btnImgUp.Visible = false;

                dal.Menu menu = e.Row.DataItem as dal.Menu;

                if (menu.ParentID.HasValue)
                {
                    // child menu
                    int maxOrder = this.DataSource.Where(m => m.ParentID == menu.ParentID).Max(m => m.Order);

                    btnImgDown.Visible = (maxOrder != menu.Order);
                }
                else
                {
                    // parent menu
                    int maxOrder = this.DataSource.Where(m => !m.ParentID.HasValue).Max(m => m.Order);

                    btnImgDown.Visible = (maxOrder != menu.Order);
                }
            }
        }

        protected void grdMenu_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int order = e.CommandArgument.ConvertToInt32();
            switch (e.CommandName)
            {
                case "Up":

                    break;

                case "Down":

                    break;
            }
        }

        protected void ctlMenuEdit_Success(object sender, EventArgs e)
        {
            this.ShowMsg(string.Format(MessageRes.Success, "Cập nhật Menu"), lblError, dal.MessageType.Success);
            modalMenuPage.Hide();
            BindMenus();
        }

        protected void ctlMenuEdit_Fail(object sender, EventArgs e)
        {
            this.ShowMsg(string.Format(MessageRes.Success, "Cập nhật Menu"), lblError, dal.MessageType.Warning);
            modalMenuPage.Hide();
        }

        protected void ctlMenuEdit_Cancel(object sender, EventArgs e)
        {
            modalMenuPage.Hide();
        }



        private List<dal.Menu> SortMenu(List<dal.Menu> menus)
        {
            //order by Parent first
            List<dal.Menu> sortedMenus = menus.Where(e => !e.ParentID.HasValue).OrderBy(e => e.Order).ToList();

            int total = sortedMenus.Count;
            for (int i = 0; i < total; i++)
            {
                dal.Menu sortedMenu = sortedMenus[i];
                List<dal.Menu> childMenus = menus.Where(e => e.ParentID.HasValue && e.ParentID == sortedMenu.MenuID).OrderByDescending(e => e.Order).ToList();

                //get position of menu
                int index = sortedMenus.IndexOf(sortedMenu);

                foreach (dal.Menu childMenu in childMenus)
                    sortedMenus.Insert(index + 1, childMenu);

                total += childMenus.Count;
                i += childMenus.Count;
            }

            return sortedMenus;
        }


        protected void grdMenu_PageIndexChanged(object sender, EventArgs e)
        {
            BindMenus();
        }

        protected void grdMenu_AddNew(object sender, EventArgs e)
        {
            ctlMenuEdit.Mode = dal.Modes.Add;
            ctlMenuEdit.LoadBlankMenuData();
            modalMenuPage.Show();
        }

        protected void grdMenu_ActionClick(object sender, ERMSys.Web.Controls.GridViewActionClickArgs e)
        {
            int menuId = e.Key.Value.ConvertToInt32();
            switch (e.ActionName)
            {
                case "Edit":
                    ctlMenuEdit.Mode = dal.Modes.Edit;
                    ctlMenuEdit.LoadMenuData(menuId);
                    modalMenuPage.Show();
                    break;

                case "Delete":
                    this.DBContext.Menu_DeleteById(menuId);
                    try
                    {
                        this.DBContext.SaveChanges();
                        this.ShowMsg(string.Format(MessageRes.Success, "Xóa"), lblError, dal.MessageType.Success);
                    }
                    catch
                    {
                        this.ShowMsg(string.Format(MessageRes.Failed, "Xóa"), lblError, dal.MessageType.Warning);
                    }
                    break;
            }
            BindMenus();
        }

        #endregion

        #region Methods

        private void BindMenus()
        {
            int total;
            grdMenu.DataSource = this.DataSource = this.SortMenu(this.DBContext.Menu_GetAll(grdMenu.PageIndex, grdMenu.PageSize, out total));
            grdMenu.TotalRecords = total;
            grdMenu.DataBind();
          
        }

        #endregion // Methods
    }
}